/**
 * Created by dqw on 2015/12/30.
 */
var id = $("#userId").val();
var parentId = $("#parentId").val();

// 银行卡列表开始
var dtGridColumnsBank = [
    {
        id: 'delFlag',
        type: 'int',
        hideQuery: true,
        'export': false,
        hideQueryType: 'eq',
        hideQueryValue: 1,
        hide: true
    },
    {
        id: 'userId',
        type: 'int',
        hideQuery: true,
        'export': false,
        hideQueryType: 'eq',
        hideQueryValue: 2,
        hide: true
    },
    {
        id: 'id',
        title: '编号',
        type: 'number',
        headerClass: 'text-center width-50',
        columnClass: 'text-center width-50',
        fastSort: true,
        fastQuery: false,
    },

    {
        id: 'bankName',
        title: '银行名称',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'branch',
        title: '支行名称',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        fastQuery: false,
    },
    {
        id: 'bankNo',
        title: '银行卡号',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        fastQuery: false,
    },
    {
        id: 'operation',
        title: '管理操作',
        type: 'string',
        'export': false,
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        extra: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var html="";
            if($("#bankEdit").val() == 1){
                html = "<a data-target='#editBank' class='btn btn-sm btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "' ><i class='fa fa-edit'></i>&nbsp;修改&nbsp;</a>";
            }
            return html;
        }
    }
];


var dtGridOptionBank = {
    lang: 'zh-cn',
    ajaxLoad: true,
    loadURL: ncGlobal.adminRoot + 'userBank/json/list',
    columns: dtGridColumnsBank,
    gridContainer: 'dtGridContainerBank',
    pageSize: 50,
    pageSizeLimit: [10, 20, 50]
};

var grid = $.fn.DtGrid.init(dtGridOptionBank);
grid.fastQueryParameters = new Object();
grid.fastQueryParameters['eq_delFlag'] = 1;
grid.fastQueryParameters['eq_userId'] = GetQueryString("userId");

//标的内容列表
var dtGridColumnsSpreader = [
    {
        id: 'delFlag',
        type: 'int',
        hideQuery:true,
        hideQueryType:'eq',
        hideQueryValue:1,
        hide:true
    },
    {
        id: 'type',
        type: 'int',
        hideQuery:true,
        hideQueryType:'eq',
        hideQueryValue:1,
        hide:true
    },
    {
        id: 'id',
        type: 'int',
        hideQuery:true,
        hideQueryType:'nin',
        hideQueryValue:[id,parentId,1],
        hide:true
    },
    {
        id: 'parentId',
        type: 'int',
        hideQuery:true,
        hideQueryType:'nin',
        hideQueryValue:id,
        hide:true
    },
    {
        id: 'userName',
        title: '用户名',
        type: 'string',
        columnClass: 'text-center',
        fastSort: false,
        fastQueryType: 'lk',
        fastQuery: true
    },
    {
        id: 'mobile',
        title: '手机号',
        type: 'int',
        columnClass: 'text-center',
        fastSort: false,
        fastQueryType: 'lk',
        fastQuery: true
    }
];

var dtGridOptionSpreader = {
    lang: 'zh-cn',
    ajaxLoad: true,
    loadURL: ncGlobal.adminRoot + '/user/json/list',
    exportFileName: '添加推广人列表',
    columns: dtGridColumnsSpreader,
    gridContainer: 'dtGridContainerSpreader',
    toolbarContainer: 'dtGridToolBarContainerSpreader',
    pageSize: 10,
    pageSizeLimit: [10, 20, 50],
    ncColumnsType: {int:["id","delFlag","type"],Timestamp:[]},
    onRowDblClick: function (value, record, column, grid, dataNo, columnNo, cell, row, extraCell, e) {

        if (record.id != null) {
            (function addParent() {
                var tpl = '您是否确定添加 <strong>' + record.userName + '</strong> 为推广人吗?'
                $.ncConfirm({
                    url: ncGlobal.adminRoot + "user/json/updateParent",
                    data: {
                        id: record.id,
                        parentId: id
                    },
                    content: tpl,
                    alertTitle: "添加操作",
                    callBack: function () {
                        window.location.reload();
                    }
                });
            })()
        }
        $("#spreaderlList").hide();//隐藏窗口
        return false;
    }
};
var grid1 = $.fn.DtGrid.init(dtGridOptionSpreader);



function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

//操作处理开始
var OperateHandle = function () {
    function _bindEvent() {

        //权限按钮显示
        //查看
        if($("#userLook").val() != 1){$("#look").hide();}
        //保存用户类型
        if($("#typeEdit").val() != 1){$("#saveType").hide();}
        //保存所属代理商
        if($("#userAgentEdit").val() != 1){$("#saveAgent").hide();}
        //手动入金
        if($("#offlineRecharge").val() != 1){$("#handRecharge").hide();}
        //手动出金
        if($("#offlineCash").val() != 1){$("#handCash").hide();}
        //修改用戶名
        if($("#userNameEdit").val() != 1){$("#saveUserName").hide();}
        //修改真实姓名
        if($("#realNameEdit").val() != 1){$("#saveRealName").hide();}
        //修改身份证号码
        if($("#idCardEdit").val() != 1){$("#saveIdCard").hide();}
        //修改佣金比例
        if($("#commissionRateEdit").val() != 1){$("#saveRewardScale").hide();}
        //修改推广人数
        if($("#inviteEdit").val() != 1){$("#invite").hide();}

        // 编辑借款标对话框
        $('#editBank').on('show.bs.modal', function (event) {
            var    //获取接受事件的元素
                button = $(event.relatedTarget),
                //获取data 参数
                datano = button.data('no'),
                modal = $(this),
                //获取列表框中的原始数据
                gridData = grid.sortOriginalDatas[datano],
                editForm = $("#editForm");
            //清除错误提示
            editForm.psly().reset();
            $(".alert-danger").remove();
            modal.find('input[name="id"]').val(gridData.id);
            modal.find('input[name="branch"]').val(gridData.branch);
            modal.find('input[name="bankNo"]').val(gridData.bankNo);
            // $("#banks option[value='"+gridData.bankCode+"']").attr("selected","selected");
            $("#banks").find("option[value="+gridData.bankCode+"]").attr("selected",true);
            // $("#banks").attr("value",gridData.bankCode);
        });

        // 推广人对话框
        $('#spreaderlList').on('show.bs.modal', function (event) {
            grid1.fastQueryParameters = new Object();
            grid1.fastQueryParameters['eq_delFlag'] = 1;
            grid1.fastQueryParameters['eq_type'] = 1;
            grid1.fastQueryParameters['nin_id'] = [id,parentId];
            grid1.fastQueryParameters['nin_parentId'] = id;
            //排序
            grid1.sortParameter.columnId = ['asc_id'];
            grid1.pager.nowPage = 1;
            grid1.load();
        });

        // 手动入金对话框
        $('#recharge').on('show.bs.modal', function (event) {
           $rechargeForm =  $("#rechargeForm");
            $rechargeForm.find('input[name="outOrderNo"]').val("");
            $rechargeForm.find('input[name="account"]').val("");
            $rechargeForm.find('input[name="remark"]').val("");
            $("#typeSource").find("option[value='1']").attr("selected",true);
            $("#typeSource").find("option[value='2']").attr("selected",false);
        });

        // 手动入金对话框
        $('#cash').on('show.bs.modal', function (event) {
            $cashForm =  $("#cashForm");
            $cashForm.find('input[name="outOrderNo"]').val("");
            $cashForm.find('input[name="account"]').val("");
            $cashForm.find('input[name="remark"]').val("");
            $("#typeSource").find("option[value='5']").attr("selected",true);
            $("#typeSource").find("option[value='6']").attr("selected",false);
            $("#typeSource").find("option[value='7']").attr("selected",false);
            $("#typeSource").find("option[value='8']").attr("selected",false);
        });

        // 保存用户类型
        $("#saveType").on("click",function (event) {
           var type  = $("#userType option:selected").val();
            $.ajax({
                type: 'POST',
                url:  ncGlobal.adminRoot + 'user/json/updateType',
                data: {
                    "id": id,
                    "type":type
                },
                success:function(req){
                    if ('200' == req.code) {
                        //弹出成功提示框
                        $.ncAlert({
                            content: "操作成功",
                            autoCloseTime: 3,
                            callback: function () {
                            }
                        });
                    } else {
                        $.ncAlert({
                            content: '<div class="alert alert-danger m-b-0">' + (req.message ? req.message : "操作失败") + '</div>',
                            autoCloseTime: 3,
                            callback: function () {
                            }
                        });
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    $.ncAlert({
                        content: '<div class="alert alert-danger m-b-0"><h4><i class="fa fa-info-circle"></i>&nbsp;连接超时</h4></div>',
                        autoCloseTime: 3,
                        callback: function () {
                        }
                    });
                }
            });
        });

        // 保存所属代理商
        $("#saveAgent").on("click",function (event) {
            var agentId  = $("#agent option:selected").val();
            $.ajax({
                type: 'POST',
                url:  ncGlobal.adminRoot + 'user/json/updateAgent',
                data: {
                    "id": id,
                    "agentId":agentId
                },
                success:function(req){
                    if ('200' == req.code) {
                        //弹出成功提示框
                        $.ncAlert({
                            content: "操作成功",
                            autoCloseTime: 3,
                            callback: function () {
                            }
                        });
                    } else {
                        $.ncAlert({
                            content: '<div class="alert alert-danger m-b-0">' + (req.message ? req.message : "操作失败") + '</div>',
                            autoCloseTime: 3,
                            callback: function () {
                            }
                        });
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    $.ncAlert({
                        content: '<div class="alert alert-danger m-b-0"><h4><i class="fa fa-info-circle"></i>&nbsp;连接超时</h4></div>',
                        autoCloseTime: 3,
                        callback: function () {
                        }
                    });
                }
            });
        });

        // 保存用户名
        $("#saveUserName").on("click",function (event) {
            var userName = $("#userName").val();
            $.ajax({
                type: 'POST',
                url:  ncGlobal.adminRoot + 'user/json/updateUserName',
                data: {
                    "id": id,
                    "userName":userName
                },
                success:function(req){
                    if ('200' == req.code) {
                        //弹出成功提示框
                        $.ncAlert({
                            content: "操作成功",
                            autoCloseTime: 3,
                            callback: function () {
                            }
                        });
                    } else {
                        $.ncAlert({
                            content: '<div class="alert alert-danger m-b-0">' + (req.message ? req.message : "操作失败") + '</div>',
                            autoCloseTime: 3,
                            callback: function () {
                            }
                        });
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    $.ncAlert({
                        content: '<div class="alert alert-danger m-b-0"><h4><i class="fa fa-info-circle"></i>&nbsp;连接超时</h4></div>',
                        autoCloseTime: 3,
                        callback: function () {
                        }
                    });
                }
            });
        });

        // 保存姓名
        $("#saveRealName").on("click",function (event) {
            var realName = $("#realName").val();
            $.ajax({
                type: 'POST',
                url:  ncGlobal.adminRoot + 'user/json/updateRealName',
                data: {
                    "id": id,
                    "realName":realName
                },
                success:function(req){
                    if ('200' == req.code) {
                        //弹出成功提示框
                        $.ncAlert({
                            content: "操作成功",
                            autoCloseTime: 3,
                            callback: function () {
                            }
                        });
                    } else {
                        $.ncAlert({
                            content: '<div class="alert alert-danger m-b-0">' + (req.message ? req.message : "操作失败") + '</div>',
                            autoCloseTime: 3,
                            callback: function () {
                            }
                        });
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    $.ncAlert({
                        content: '<div class="alert alert-danger m-b-0"><h4><i class="fa fa-info-circle"></i>&nbsp;连接超时</h4></div>',
                        autoCloseTime: 3,
                        callback: function () {
                        }
                    });
                }
            });
        });

        // 保存身份证号
        $("#saveIdCard").on("click",function (event) {
            var idCard = $("#idCard").val();
            $.ajax({
                type: 'POST',
                url:  ncGlobal.adminRoot + 'user/json/updateIdCard',
                data: {
                    "id": id,
                    "idCard":idCard
                },
                success:function(req){
                    if ('200' == req.code) {
                        //弹出成功提示框
                        $.ncAlert({
                            content: "操作成功",
                            autoCloseTime: 3,
                            callback: function () {
                            }
                        });
                    } else {
                        $.ncAlert({
                            content: '<div class="alert alert-danger m-b-0">' + (req.message ? req.message : "操作失败") + '</div>',
                            autoCloseTime: 3,
                            callback: function () {
                            }
                        });
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    $.ncAlert({
                        content: '<div class="alert alert-danger m-b-0"><h4><i class="fa fa-info-circle"></i>&nbsp;连接超时</h4></div>',
                        autoCloseTime: 3,
                        callback: function () {
                        }
                    });
                }
            });
        });


        // 保存推广佣金
        $("#saveRewardScale").on("click",function (event) {
            var rewardScale = $("#rewardScale").val();
            $.ajax({
                type: 'POST',
                url:  ncGlobal.adminRoot + 'user/json/updateReward',
                data: {
                    "id": id,
                    "rewardScale":rewardScale
                },
                success:function(req){
                    if ('200' == req.code) {
                        //弹出成功提示框
                        $.ncAlert({
                            content: "操作成功",
                            autoCloseTime: 3,
                            callback: function () {
                            }
                        });
                    } else {
                        $.ncAlert({
                            content: '<div class="alert alert-danger m-b-0">' + (req.message ? req.message : "操作失败") + '</div>',
                            autoCloseTime: 3,
                            callback: function () {
                            }
                        });
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    $.ncAlert({
                        content: '<div class="alert alert-danger m-b-0"><h4><i class="fa fa-info-circle"></i>&nbsp;连接超时</h4></div>',
                        autoCloseTime: 3,
                        callback: function () {
                        }
                    });
                }
            });
        });

        // 查看
        $('#look').click(function (event) {
            $("#showMobile").text($("#mobile").val())
            $("#showMobile").append('<button id="look"class="btn btn-default btn-s">查看</button>')
        })



    }

    //外部可调用
    return {
        bindEvent: _bindEvent
    }
}();
//操作处理结束

$(function () {
    //页面绑定事件
    OperateHandle.bindEvent();
    grid.load();
});